﻿using System;
using System.Collections.Generic;
using System.Text;

namespace TCCHWLib.Commands
{
    public class TxWriteCommand: ICommand
    {

        public TxWriteCommand(object parameter1, ParameterType paramterType1,object parameter2,ParameterType parameterType2,object index,ParameterType parameterType3)
        {
            m_Operand1 = parameter1;
            m_Operand2 = parameter2;
            m_Operand3 = index;
            m_Type1 = paramterType1;
            m_Type2 = parameterType2;
            m_Type3 = parameterType3;
            m_ExOperand1 = parameter1;
            m_ExOperand2 = parameter2;
            m_ExOperand3 = index;
            m_ExType1 = paramterType1;
            m_ExType2 = parameterType2;
            m_ExType3 = parameterType3;
            this.Value = false;
        }

        public override object Execute(Processor processor)
        {
            int address = Convert.ToInt32(m_ExOperand1);
            int index = Convert.ToInt32(m_ExOperand3);
            address += index;
            processor.Cache.ReadWriteSet(address);
            //Chip.Instance.Bus.CheckWriteSignature(address);
            processor.WriteSet.Write(address, m_ExOperand2);
            processor.WriteSignatureRegister.Insert(address);
            return true;
        }
        public override string ToString()
        {
            string val = "";
            switch (m_Type2)
            {
                case ParameterType.Value:
                    val = "{" + m_Operand2 + "}";
                    break;
                case ParameterType.Address:
                    val = "[" + m_Operand2 + "]";
                    break;
                case ParameterType.Instruction:
                    val = "i" + m_Operand2;
                    break;
                case ParameterType.Null:    
                    val = "NULL";
                    break;
                default:
                    val = m_Operand2.ToString();
                    break;
            }

            return String.Format("TX-WRITE ( {0}{1} , {2} )", m_Operand1, ((m_Operand3 != null) ? " + " + m_Operand3 : ""), val);
            //return base.ToString() + String.Format(" > ADDR={0}, VAL={1} , next pc {2}", m_Operand1, m_Operand2, NextPC);
        }
    }
}
